import requests
import re
import csv

from requests import RequestException


def get_page(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        return response.text
    except RequestException as e:
        print(f"Error fetching {url}: {e}")
        return None


def get_movie_list(html):
    pattern = re.compile(r'<div class="pic".*?<em class="">(.*?)</em>.*?<a href="(.*?)".*?>', re.S)
    movie_list = pattern.findall(html)
    return movie_list


def get_content(movie):
    movie_url = movie[1]
    html = get_page(movie_url)
    if html is None:
        return None


    name_pattern = re.compile(r'<span property="v:itemreviewed">(.*?)</span>', re.S)
    name = name_pattern.findall(html)

    director_pattern = re.compile(r'<a.*?rel="v:directedBy">(.*?)</a>', re.S)
    director = director_pattern.findall(html)

    author_pattern_1 = re.compile(r"<span ><span class='pl'>编剧</span>: <span class='attrs'>(.*?)</span></span><br/>",
                                  re.S)
    author_raw = author_pattern_1.findall(html)
    if author_raw:
        author_pattern_2 = re.compile(r'<a href=.*?>(.*?)</a>', re.S)
        author = author_pattern_2.findall(author_raw[0])
    else:
        author = []

    actor_pattern = re.compile(r'<a.*?rel="v:starring">(.*?)</a>', re.S)
    actor = actor_pattern.findall(html)

    movie_type_pattern = re.compile(r'<span property="v:genre">(.*?)</span>', re.S)
    movie_type = movie_type_pattern.findall(html)

    area_pattern = re.compile(r'<span class="pl">制片国家/地区:</span>(.*?)<br/>', re.S)
    area = area_pattern.findall(html)

    language_pattern = re.compile(r'<span class="pl">语言:</span>(.*?)<br/>', re.S)
    language = language_pattern.findall(html)

    movie_info = {
        'Name': name[0] if name else '',
        'Director': director[0] if director else '',
        'Author': ', '.join(author) if author else '',
        'Actor': ', '.join(actor) if actor else '',
        'Type': ', '.join(movie_type) if movie_type else '',
        'Area': ', '.join(area) if area else '',
        'Language': ', '.join(language) if language else ''
    }
    return movie_info


def write_to_csv(movies_info, filename='3.movies.csv'):
    with open(filename, mode='w', newline='', encoding='utf-8') as file:
        writer = csv.DictWriter(file, fieldnames=['Name', 'Director', 'Author', 'Actor', 'Type', 'Area', 'Language'])
        writer.writeheader()
        for movie_info in movies_info:
            writer.writerow(movie_info)


def main():
    url = 'https://movie.douban.com/top250?start=0&filter='
    html = get_page(url)
    if html:
        movie_list = get_movie_list(html)
        movies_info = []
        for movie in movie_list:
            movie_info = get_content(movie)
            if movie_info:
                movies_info.append(movie_info)
        write_to_csv(movies_info)
        print("Data written to CSV successfully.")


if __name__ == "__main__":
    main()